package com.zsk.shop.api.idcard;

import com.alibaba.fastjson.JSON;
import com.zsk.shop.common.utils.HttpUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AliYunTrueNameCheck {

    /**
     * @param idCard
     * @param name
     * @return true 匹配 false 不匹配
     */
    public Boolean check(String idCard, String name) {
        String host = "https://zid.market.alicloudapi.com";
        String path = "/idcard/VerifyIdcardv2";
        String method = "GET";
        String appcode = "1016e19ac0b6402e93a360a7cb4a62dd";
        Map<String, String> headers = new HashMap<>(1);
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<>(2);
        querys.put("cardNo", idCard);
        querys.put("realName", name);
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtil.doGet(host, path, method, headers, querys);
//            System.out.println(response.toString());
            //获取response的body
            AliYunTrueNameDate date = JSON.parseObject(EntityUtils.toString(response.getEntity()), AliYunTrueNameDate.class);
            AliYunTrueNameResult result = date.getResult();
            if (result.getIsok()) {
                return true;
            } else {
                return false;
            }
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
